package com.zzyl.nursing.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.domain.Dto.NursingPlanDTO;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.vo.NursingPlanVo;
import com.zzyl.nursing.domain.vo.NursingProjectPlanVo;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-02
 */
@Transactional
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {

        //查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
//      Long planId=nursingPlan.getId();

        BeanUtils.copyProperties(nursingPlan,nursingPlanVo);
        List<NursingProjectPlanVo> list=  nursingProjectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;

    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param
     * @return 结果
     */
    @Override

    public int insertNursingPlan(NursingPlanDTO nursingPlanDTO)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDTO,nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

       int count= nursingProjectPlanMapper.batchInsert(nursingPlanDTO.getProjectPlans(),nursingPlan.getId());
        return count==0?0:1;
    }

    /**
     * 修改护理计划
     * 
     * @param
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDTO nursingPlanDTO)
    {
        try {
            NursingPlan  nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(nursingPlanDTO,nursingPlan);
            nursingPlan.setUpdateTime(DateUtils.getNowDate());

            if (nursingPlanDTO.getProjectPlans()!=null && nursingPlanDTO.getProjectPlans().size()>0){
                nursingProjectPlanMapper.deleteByPlanId(nursingPlanDTO.getId());
                nursingProjectPlanMapper.batchInsert(nursingPlanDTO.getProjectPlans(),nursingPlan.getId());

            }
            //别管项目列表是否为空，都要修改护理计划
            return nursingPlanMapper.updateNursingPlan(nursingPlan);
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        try {
            nursingPlanMapper.deleteNursingPlanById(id);
            return  nursingProjectPlanMapper.deleteNursingProjectPlanById(id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
